package cn.iocoder.yudao.module.shop.controller.admin.orderfeedback;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.orderfeedback.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.orderfeedback.ShopOrderFeedbackDO;
import cn.iocoder.yudao.module.shop.convert.orderfeedback.ShopOrderFeedbackConvert;
import cn.iocoder.yudao.module.shop.service.orderfeedback.ShopOrderFeedbackService;

@Tag(name = "管理后台 - 顾客订单反馈")
@RestController
@RequestMapping("/shop/order-feedback")
@Validated
public class ShopOrderFeedbackController {

    @Resource
    private ShopOrderFeedbackService orderFeedbackService;

    @PostMapping("/create")
    @Operation(summary = "创建顾客订单反馈")
    @PreAuthorize("@ss.hasPermission('shop:order-feedback:create')")
    public CommonResult<Long> createOrderFeedback(@Valid @RequestBody ShopOrderFeedbackCreateReqVO createReqVO) {
        return success(orderFeedbackService.createOrderFeedback(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新顾客订单反馈")
    @PreAuthorize("@ss.hasPermission('shop:order-feedback:update')")
    public CommonResult<Boolean> updateOrderFeedback(@Valid @RequestBody ShopOrderFeedbackUpdateReqVO updateReqVO) {
        orderFeedbackService.updateOrderFeedback(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除顾客订单反馈")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:order-feedback:delete')")
    public CommonResult<Boolean> deleteOrderFeedback(@RequestParam("id") Long id) {
        orderFeedbackService.deleteOrderFeedback(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得顾客订单反馈")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:order-feedback:query')")
    public CommonResult<ShopOrderFeedbackRespVO> getOrderFeedback(@RequestParam("id") Long id) {
        ShopOrderFeedbackDO orderFeedback = orderFeedbackService.getOrderFeedback(id);
        return success(ShopOrderFeedbackConvert.INSTANCE.convert(orderFeedback));
    }

    @GetMapping("/list")
    @Operation(summary = "获得顾客订单反馈列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:order-feedback:query')")
    public CommonResult<List<ShopOrderFeedbackRespVO>> getOrderFeedbackList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopOrderFeedbackDO> list = orderFeedbackService.getOrderFeedbackList(ids);
        return success(ShopOrderFeedbackConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得顾客订单反馈分页")
    @PreAuthorize("@ss.hasPermission('shop:order-feedback:query')")
    public CommonResult<PageResult<ShopOrderFeedbackRespVO>> getOrderFeedbackPage(@Valid ShopOrderFeedbackPageReqVO pageVO) {
        PageResult<ShopOrderFeedbackDO> pageResult = orderFeedbackService.getOrderFeedbackPage(pageVO);
        return success(ShopOrderFeedbackConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出顾客订单反馈 Excel")
    @PreAuthorize("@ss.hasPermission('shop:order-feedback:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderFeedbackExcel(@Valid ShopOrderFeedbackExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopOrderFeedbackDO> list = orderFeedbackService.getOrderFeedbackList(exportReqVO);
        // 导出 Excel
        List<ShopOrderFeedbackExcelVO> datas = ShopOrderFeedbackConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "顾客订单反馈.xls", "数据", ShopOrderFeedbackExcelVO.class, datas);
    }

}
